from .models import *

from django.contrib import admin


admin.site.register(SubCompany)
admin.site.register(RelationLevel)
admin.site.register(TransportType)
admin.site.register(TypeCategory)
admin.site.register(EquipmentType)
admin.site.register(EquipmentUnit)
admin.site.register(BankAccount)
admin.site.register(Salesman)
admin.site.register(TransferRecord)
admin.site.register(PowerType)
admin.site.register(OrderNote)
admin.site.register(InstallationDesc)
admin.site.register(PayMethod)
admin.site.register(InstallationMaterial)
admin.site.register(ExportPrice)
admin.site.register(OrderState)
admin.site.register(ServiceState)
admin.site.register(ServiceRecord)


@admin.register(EquipmentSize)
class EquipmentSizeAdmin(admin.ModelAdmin):
    list_display = ('equipment_size_en', 'equipment_size_cn')
    search_fields = ['equipment_size_en', 'equipment_size_cn']


@admin.register(Equipment)
class EquipmentAdmin(admin.ModelAdmin):
    list_display = ('equipment_code', 'pk', 'equipment_exp_code', 'equipment_en', 'equipment_cn', 'size_para', 'weight',
                    'basic_price', 'power', 'equipment_type', 'datetime_created', 'datetime_updated')
    list_filter = ('equipment_type', )
    filter_horizontal = ('related_equipment', 'equipment_size')
    list_editable = ('equipment_en', 'equipment_cn', 'power', 'equipment_exp_code')
    search_fields = ['equipment_code', 'equipment_en', 'equipment_cn', 'equipment_exp_code', 'related_materials',
                     'equipment_para_en', 'equipment_para_cn']
    readonly_fields = ('equipment_code_auto',)


@admin.register(Client)
class ClientAdmin(admin.ModelAdmin):
    list_display = ('name', 'company', 'company_address', 'bank_account')
    search_fields = ['name', 'company', 'company_address', 'bank_account']


@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
    list_display = ('order_code', 'order_desc', 'client', 'salesman', 'sub_company', 'total_price', 'total_discount')
    list_filter = ('salesman', 'client', 'sub_company', 'is_active', 'is_success')
    search_fields = ['order_code', 'order_desc', 'client__name', 'client__company']
    filter_horizontal = ('cooperators',)


@admin.register(ItemList)
class ItemListAdmin(admin.ModelAdmin):
    list_display = ('order', 'order_index', 'equipment', 'initial_price_each', 'amount',
                    'item_final_price', 'datetime_updated')
    list_filter = ('order', 'equipment')
    list_editable = ('order_index', 'amount', 'initial_price_each',)
    search_fields = ['order__order_desc', 'order__order_code', 'equipment__equipment_cn', 'equipment__equipment_en']


@admin.register(AnnounceTag)
class AnnounceTagAdmin(admin.ModelAdmin):
    list_display = ('announce_tag', 'datetime_created', 'datetime_updated')
    search_fields = ['announce_tag', ]


@admin.register(Announce)
class AnnounceAdmin(admin.ModelAdmin):
    list_display = ('title', 'created_by', 'datetime_created', 'datetime_updated')
    search_fields = ['title', 'remarks']


# @admin.register(EquipmentList)
# class EquipmentListAdmin(admin.ModelAdmin):
#     list_display = ('pk', 'equipment_list', 'is_show', 'datetime_created', 'datetime_updated')


@admin.register(Warehouse)
class WarehouseAdmin(admin.ModelAdmin):
    list_display = ('pk', 'name', 'desc', 'datetime_created', 'datetime_updated')


@admin.register(EquipmentIn)
class EquipmentInAdmin(admin.ModelAdmin):
    list_display = ('pk', 'warehouse', 'equipment', 'amount', 'datetime_created', 'datetime_updated')


@admin.register(EquipmentOut)
class EquipmentOutAdmin(admin.ModelAdmin):
    list_display = ('pk', 'warehouse', 'equipment', 'amount', 'is_confirmed', 'datetime_created', 'datetime_updated')


@admin.register(EquipmentAssembly)
class EquipmentAssemblyAdmin(admin.ModelAdmin):
    list_display = ('pk', 'assembly_name', 'equipment', 'is_confirmed', 'datetime_created', 'datetime_updated')
    filter_horizontal = ('cooperators',)


@admin.register(AssemblyItem)
class AssemblyItemAdmin(admin.ModelAdmin):
    list_display = ('pk', 'equipment_assembly', 'equipment', 'amount', 'datetime_created', 'datetime_updated')


@admin.register(PlanAssembly)
class PlanAssemblyAdmin(admin.ModelAdmin):
    list_display = ('pk', 'related_equipment_assembly', 'amount', 'created_by', 'is_ordered',
                    'datetime_created', 'datetime_updated')


@admin.register(FixedAssetsType)
class FixedAssetsTypeAdmin(admin.ModelAdmin):
    list_display = ('pk', 'fixed_assets_type', 'notes', 'datetime_created', 'datetime_updated')


@admin.register(FixedAssetsOut)
class FixedAssetsOutAdmin(admin.ModelAdmin):
    list_display = ('fixed_assets', 'amount', 'date_of_out', 'is_confirmed')
    list_filter = ('fixed_assets', 'is_confirmed')


@admin.register(InOutType)
class InOutTypeAdmin(admin.ModelAdmin):
    list_display = ('pk', 'in_out_type', 'is_out')


@admin.register(InOut)
class InOutAdmin(admin.ModelAdmin):
    list_display = ('in_out_date', 'amount', 'desc', 'linked_order', 'invoice', 'datetime_updated')
    list_filter = ('in_out_type', 'is_confirmed')
    search_fields = ['desc', ]

    def get_queryset(self, request):
        qs = super(InOutAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(created_by_id=request.user.id)


@admin.register(FixedAssets)
class FixedAssetsAdmin(admin.ModelAdmin):
    list_display = ('name_of_asset', 'specifications', 'fixed_assets_type', 'datetime_created', 'datetime_updated')
    list_filter = ('fixed_assets_type', )


@admin.register(FixedAssetsIn)
class FixedAssetsInAdmin(admin.ModelAdmin):
    list_display = ('fixed_assets', 'price_each', 'amount', 'date_of_purchase', 'warranty_period')
    list_filter = ('fixed_assets', 'is_confirmed')
